interface State {
	data: any[]
}

const initState: State = {
	data: []
}


const reducer = (state = initState, action: any) => {
	switch (action.type) {
		case 'addCar': {
			const item = state.data.find(v => v.id === action.payload.id);
			if (item) {
				item.count++
			} else {
				state.data.push({ ...action.payload, count: 1, isChecked: false })
			}
			return {
				...state,
				data: [...state.data]
			}
		}
		case 'addCount': {
			const item = state.data.find(v => v.id === action.payload.id);
			if (item) {
				item.count++
			}
			return {
				...state,
				data: [...state.data]
			}
		}
		case 'subCount': {
			const item = state.data.find(v => v.id === action.payload.id);
			if (item) {
				item.count--
			}
			return {
				...state,
				data: [...state.data]
			}
		}
		case 'checkOne': {
			const item = state.data.find(v => v.id === action.payload.id);
			if (item) {
				item.isChecked = !item.isChecked
			}
			return {
				...state,
				data: [...state.data]
			}
		}
		default: return state;
	}
	return state
}


export default reducer